package router

import (
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/controller/admin"
	"yunj/pkg/yunj/app/controller/admin/auth"
	"yunj/pkg/yunj/app/controller/admin/corn"
	"yunj/pkg/yunj/app/controller/admin/department"
	"yunj/pkg/yunj/app/controller/admin/member"
	"yunj/pkg/yunj/app/controller/admin/member/log"
	"yunj/pkg/yunj/app/controller/admin/notice"
	"yunj/pkg/yunj/app/controller/admin/postion"
	"yunj/pkg/yunj/app/controller/admin/queue"
	"yunj/pkg/yunj/app/controller/admin/role"
	"yunj/pkg/yunj/app/controller/admin/task"
	adminMiddleware "yunj/pkg/yunj/app/middleware/admin"

	"github.com/gin-gonic/gin"
)

// 后台路由
func AdminRouter(router *gin.Engine) {
	adminRouter := router.Group(global.Config.GetAppAdminEntrance())
	{
		// 错误捕获和日志中间件
		adminRouter.Use(
			gin.RecoveryWithWriter(gin.DefaultErrorWriter, func(c *gin.Context, recovered any) {
				adminMiddleware.Recover(c, recovered)
			}),
			adminMiddleware.Log(),
		)
		otherController := admin.Other{}
		// 不需要登录鉴权
		r1 := *adminRouter
		{
			// 空请求
			r1.Any("/empty", otherController.Empty)
			// 提示页
			r1.GET("/tips", otherController.Tips)
			// 图片验证码
			r1.POST("/captcha", otherController.Captcha)
			// 登录
			loginController := member.Login{}
			r1.Any("/login", loginController.Login)
		}

		// 需要登录鉴权
		r2 := *adminRouter
		{
			// 登录鉴权中间件
			r2.Use(adminMiddleware.Auth())
			// 首页
			indexController := admin.Index{}
			r2.GET("/", indexController.Index)
			// 退出登录
			loginController := member.Login{}
			r2.Any("/logout", loginController.Logout)
			// 文件上传
			r2.Any("/file/upload", otherController.FileUpload)

			// 通知管理
			noticeRouter := r2.Group("/notice")
			{
				noticeController := notice.Notice{}
				// 顶部通知数据
				noticeRouter.POST("/top-data", noticeController.TopData)
				// 未读通知列表
				noticeRouter.POST("/unread-list", noticeController.GetUnreadList)
				// 已读
				noticeRouter.POST("/read", noticeController.Read)
			}

			// 成员管理
			memberRouter := r2.Group("/member")
			{
				memberController := member.Member{}
				// 列表
				memberRouter.Any("/list", memberController.List)
				// 添加
				memberRouter.Any("/list/add", memberController.Add)
				// 编辑
				memberRouter.Any("/list/edit", memberController.Edit)
				// 个人信息
				memberRouter.Any("/info", memberController.Info)
				// 部门岗位下拉搜索可选项
				memberRouter.GET("/departmentPostionDropdownSearchOptions", memberController.DepartmentPostionDropdownSearchOptions)
				// 部门岗位的角色ids
				memberRouter.POST("/departmentPostionRoleIds", memberController.DepartmentPostionRoleIds)
				// 下拉搜索可选项
				memberRouter.GET("/dropdownSearchOptions", memberController.DropdownSearchOptions)
				// 日志
				logController := log.Log{}
				// 操作日志
				memberRouter.Any("/log/list", logController.List)
				// 登录日志
				memberRouter.Any("/login-log/list", logController.LoginList)
			}

			// 角色管理
			roleRouter := r2.Group("/role")
			{
				roleController := role.Role{}
				// 列表
				roleRouter.Any("/list", roleController.List)
				// 添加
				roleRouter.Any("/list/add", roleController.Add)
				// 编辑
				roleRouter.Any("/list/edit", roleController.Edit)
				// 排序
				roleRouter.Any("/list/sort", roleController.Sort)
				// 下拉搜索可选项
				roleRouter.Any("/dropdownSearchOptions", roleController.DropdownSearchOptions)
			}

			// 部门管理
			departmentRouter := r2.Group("/department")
			{
				departmentController := department.Department{}
				// 列表
				departmentRouter.Any("/list", departmentController.List)
				// 添加
				departmentRouter.Any("/list/add", departmentController.Add)
				// 编辑
				departmentRouter.Any("/list/edit", departmentController.Edit)
				// 排序
				departmentRouter.Any("/list/sort", departmentController.Sort)
				// 父权限下拉搜索可选项
				departmentRouter.GET("/pidDropdownSearchOptions", departmentController.PidDropdownSearchOptions)
				// 下拉搜索可选项
				departmentRouter.GET("/dropdownSearchOptions", departmentController.DropdownSearchOptions)
			}

			// 部门岗位管理
			departmentPostionRouter := r2.Group("/department-postion")
			{
				departmentPostionController := department.Postion{}
				// 列表
				departmentPostionRouter.Any("/list", departmentPostionController.List)
				// 添加
				departmentPostionRouter.Any("/list/add", departmentPostionController.Add)
				// 编辑
				departmentPostionRouter.Any("/list/edit", departmentPostionController.Edit)
				// 岗位下拉搜索可选项
				departmentPostionRouter.GET("/postionDropdownSearchOptions", departmentPostionController.PostionDropdownSearchOptions)
			}

			// 岗位管理
			postionRouter := r2.Group("/postion")
			{
				postionController := postion.Postion{}
				// 列表
				postionRouter.Any("/list", postionController.List)
				// 添加
				postionRouter.Any("/list/add", postionController.Add)
				// 编辑
				postionRouter.Any("/list/edit", postionController.Edit)
				// 排序
				postionRouter.Any("/list/sort", postionController.Sort)
				// 下拉搜索可选项
				postionRouter.GET("/dropdownSearchOptions", postionController.DropdownSearchOptions)
			}

			// 权限管理
			authRouter := r2.Group("/auth")
			{
				authController := auth.Auth{}
				// 列表
				authRouter.Any("/list", authController.List)
				// 添加
				authRouter.Any("/list/add", authController.Add)
				// 编辑
				authRouter.Any("/list/edit", authController.Edit)
				// 排序
				authRouter.Any("/list/sort", authController.Sort)
				// 父权限下拉搜索可选项
				authRouter.GET("/parentKeyDropdownSearchOptions", authController.ParentKeyDropdownSearchOptions)
			}

			// 任务管理
			taskRouter := r2.Group("/task")
			{
				taskController := task.Task{}
				// 列表
				taskRouter.Any("/list", taskController.List)
			}

			// 队列管理
			queueRouter := r2.Group("/queue")
			{
				queueController := queue.Queue{}
				// 列表
				queueRouter.Any("/list", queueController.List)
			}

			// 定时任务
			cornRouter := r2.Group("/corn")
			{
				cornController := corn.Corn{}
				// 列表
				cornRouter.Any("/list", cornController.List)
			}
		}
	}

}
